package org.example.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.example.constants.CommonConstants;
import org.example.utils.JwtUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.UUID;

import static org.example.constants.CommonConstants.API_HEADER_NAME;
import static org.example.constants.CommonConstants.API_INTERNAL_HEADER_NAME;

/**
 * Feign 请求拦截器，用于为所有 Feign 调用添加请求头
 */
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {

    /**
     * 应用拦截器逻辑
     * @param template Feign 请求模板
     */
    @Override
    public void apply(RequestTemplate template) {
        // 1. 添加自定义固定请求头
        //template.header("X-Feign-Client", "Java-Service");
        
        // 2. 传递当前请求的认证令牌
        addAuthorizationHeader(template);

        addInternalApiHeader1(template);

        // 3. 传递当前请求的跟踪ID
        //addTraceIdHeader(template);

        // 4. 传递原始请求的其他头信息
        propagateOriginalHeaders(template);
    }

    private void addInternalApiHeader1(RequestTemplate template) {
        template.header(
        API_INTERNAL_HEADER_NAME, JwtUtil.createApiToken(CommonConstants.ServiceName.ACCOUNT_SERVICE.getServiceName(), UUID.randomUUID().toString()));
    }

    /**
     * 添加认证令牌头
     */
    private void addAuthorizationHeader(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)
            RequestContextHolder.getRequestAttributes();

        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            String authHeader = request.getHeader(API_HEADER_NAME);
            if (authHeader != null) {
                template.header(API_HEADER_NAME, authHeader);
            }
        }
    }


    
    /**
     * 传递原始请求的头信息
     */
    private void propagateOriginalHeaders(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) 
            RequestContextHolder.getRequestAttributes();
        
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                
                // 跳过已处理的头信息
                if (API_HEADER_NAME.equalsIgnoreCase(headerName) ||
                    API_INTERNAL_HEADER_NAME.equalsIgnoreCase(headerName)) {
                    continue;
                }
                
                // 传递其他头信息
                Enumeration<String> values = request.getHeaders(headerName);
                while (values.hasMoreElements()) {
                    String value = values.nextElement();
                    template.header(headerName, value);
                }
            }
        }
    }
}